<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Common;
use app\service\orderService;
use app\service\apiService;

use printing\printing;
use think\Db;
use think\db\Where;
use think\Request;
use think\Response;
class Carownerwelfare extends Common{

    public function carlist(){
		 //根据当前账号查询区域
        $com = $this->pidTokenId();
        $whe=new where();
        $whe['community_id'] = array('in',implode(',',$com['community_id']));
        //根据账号查询停车场
        $parkinglist = (new orderService())->parkingList($whe);
        $this->assign('parkinglist',$parkinglist);
		 //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $parkingNo[] = $park['parkingNo'];
            }
            //将数组转换成字符串
            $parkingNo = implode(',',$parkingNo);
        }else{
            $parkingNo = null;
        }
        $community_id = $com['community_id'];
        //搜索
        $data = request()->get();
        // dump($data);
        if (!empty($data)){
            if(!empty($data['parkingNo'])){
                $where['parkingNo'] = $data['parkingNo'];//停车场名称
            }
            if(!empty($data['car_number'])){
               $where[]=['car_number','like','%'.$data['car_number'].'%'];
            }
            if(!empty($data['address'])){
                $where[]=['address','like','%'.$data['address'].'%'];
            }
			
            if(!empty($data['owner_phone'])){
                $where['owner_phone'] = $data['owner_phone'];//车主电话
            }
			if(!empty($data['owner_name'])){
                $where['owner_name'] = $data['owner_name'];//车主电话
            }
            
            $this->assign('where',$where);   
            $where = array_filter($where);
        }else{
            $where=array();
        }
        $export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        $this->assign('export',$export);
        $page = request()->has('page') ? request()->get('page') : 1;

        $carlist = Db::name('car_welfare_owner')->where('parkingNo', 'in', $parkingNo)->where($where)->order('id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
        //print_r($where);
        $number = Db::name('car_welfare_owner')->where('parkingNo', 'in', $parkingNo)->where($where)->count();;
        $this->assign('number',$number);
		
        $this->assign('carlist',$carlist);
		$this->assign('list',$carlist);
        foreach ($data as $k => $v) {
            $this->assign($k, $v);
        }

        return $this->fetch('carlist');
	}
	
	public function caradd(){
		//根据当前账号查询区域
        $com = $this->pidTokenId();
        $whe=new Where();
        $whe['community_id'] = array('in',implode(',',$com['community_id']));
        $parkinglist = (new orderService())->parkingList($whe);
        $this->assign('parkinglist',$parkinglist);

        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $roominfo=Db::table("roominfo")->where("id",$data["room_id"])->find();
            if(empty($roominfo)){
                $this->error("房屋错误");
            }
            //echo "<pre>";
           // var_dump($data);die;
			$data["car_number"]=trim($data["car_number"]);
            $data['create_time'] = time();
			
			//查询该车辆是否已经添加
			$carinfo=Db::table("car_welfare_owner")->where("parkingNo",$roominfo["parkingNo"])->where("car_number",$data["car_number"])->find();
			if(!empty($carinfo))
			{
				$this->error('该车辆已经添加',url('caradd'));
			}
			$data["parkingName"]=$roominfo["parkingName"];
            $data["parkingNo"]=$roominfo["parkingNo"];
            $data["owner_name"]=$roominfo["owner_name"];
            $data["owner_phone"]=$roominfo["phone"];
            $data["address"]=$roominfo["group"].$roominfo["building"]."幢".$roominfo["unit"]."单元".$roominfo["room"]."室";
			$data['operator'] = session("name");
            //插入数据到数据库
            $res = Db::table("car_welfare_owner")->insert($data);
           
            if($res)
			{
                $this->success('车辆添加成功',url('caradd'));
            }else
			{
                $this->error('车辆添加失败',url('caradd'));
            }
        }else{
            $room_id=request()->get("room_id");
            $roominfo=Db::table("roominfo")->where("id",$room_id)->find();
            $this->assign("roominfo",$roominfo);
			return $this->fetch('');
		}
       
	}
	
	public function caredit(){
		//根据当前账号查询区域
        $com = $this->pidTokenId();
        $whe=new Where();
        $whe['community_id'] = array('in',implode(',',$com['community_id']));
        $parkinglist = (new orderService())->parkingList($whe);
        $this->assign('parkinglist',$parkinglist);

        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
			$data["car_number"]=trim($data["car_number"]);
			$carownerinfo=Db::table("car_owner_welfare")->where("id",$data["id"])->find();
			//查询该车辆是否已经添加
			$carinfo=Db::table("car_welfare_owner")->where("parkingNo",$carownerinfo["parkingNo"])->where("car_number",$data["car_number"])->where("id","neq",$data["id"])->find();
			if(!empty($carinfo))
			{
				$this->error('该车辆已经添加',url('caredit'));
			}
			
			
            //插入数据到数据库
            $res = Db::table("car_owner_welfare")->update($data);
           
            if($res)
			{
				$carownerinfo["operate_name"]=session("name");
				$carownerinfo["action"]="1";
				$carownerinfo["operate_time"]=time();
				Db::table("car_welfare_owner_operate")->insert($carownerinfo);
                $this->success('车辆修改成功',url('caradd'));
            }else
			{
                $this->error('车辆修改失败',url('caradd'));
            }
        }else{
			$id=request()->get("id");
			$carinfo=Db::table("car_welfare_owner")->where("id",$id)->find();
			$this->assign("carinfo",$carinfo);
			return $this->fetch('');
		}
       
	}
	
	
	public function cardel(){
		$id=request()->get("id");
		$carinfo=Db::table("car_welfare_owner")->where('id',$id)->find();
		
		$carinfo["operate_name"]=session("name");
		$carinfo["action"]="2";
		$carinfo["operate_time"]=time();
		Db::table("car_welfare_owner_operate")->insert($carinfo);
		$bool= Db::name('car_welfare_owner')->delete($id);
		if($bool){
			$this->success('删除成功',url('carlist'));
		}else{
			$this->error('删除失败',url('carlist'));
		}
	}
	
	public function orderinfo()
	{
		$com = $this->pidTokenId();
		$id=request()->get("id");
		$carinfo=Db::table("car_welfare_owner")->where("id",$id)->find();
		$data = request()->get();
		//var_dump($data);
        if (!empty($data)){
			if(!empty($data['in_year'])){
                $where['in_year'] = $data['in_year'];
            }else{
                $where['in_year'] = "";
            }
            if(!empty($data['in_month'])){
                $where['in_month'] = $data['in_month'];
            }else{
                $where['in_month'] = "";
            }
            $this->assign('where',$where);   
            $where = array_filter($where);
        }else{
            $where=array();
        }
		$where["parkingNo"]=$carinfo["parkingNo"];
		$where["parkingName"]=$carinfo["parkingName"];
		$where["pstatus"]="1";
		//var_dump($where);
		$page = request()->has('page') ? request()->get('page') : 1;
		//查询该车的停车记录
		$orderwelfare=Db::table("car_welfare_order")
							->where($where)
							->order("id desc")
							->paginate(100, false, [
								'page'  =>  $page,
								'query' =>  $where
							]);
		$feetime=Db::table("car_welfare_order")
							->where($where)
							->sum("fee_duration");
		$this->assign("feetime",$feetime);
		$this->assign("orderwelfare",$orderwelfare);
		foreach ($data as $k => $v) {
            $this->assign($k, $v);
        }
        $this->assign("id",$id);
		return $this->fetch();
	}
}